<?php

class zdBanerComponents extends sfComponents {
	
	public function executeShow_baner() {

		
		$c = new Criteria();
		$c->add(Baner2FolderPeer::FOLDER_ID,$this->folder_id);
		$c->addJoin(Baner2FolderPeer::BANER_ID,BanerPeer::ID);
		$c->add(BanerPeer::POSITION_ID,$this->position);
		$c->addAscendingOrderByColumn('RAND()');
		$cnt = Baner2FolderPeer::doCount($c);
		if($cnt>0)
		{
			$this->baner = BanerPeer::doSelectOne($c);
			$this->type = $this->Gettype( $this->baner->getFilePath());
			if($this->baner)
			{
			$this->baner->setSee($this->baner->getSee()+1);
			$this->baner->save();
			}
			return sfView::SUCCESS;
		}
		
		$c = new Criteria();
		$c->add(BanerPeer::POSITION_ID,$this->position);
		$c->addAnd(BanerPeer::PUB,true);
		$c->addAnd(BanerPeer::IS_FOLDER,false);
		
		   if(sfConfig::get('app_use_simple_region')==true)
        {
        	die;
        	if(sfContext::getInstance()->getUser()->getAttribute('city_id')!=1)
        	{
				$crit1=$c->getNewCriterion(BanerPeer::REGION_ID,sfContext::getInstance()->getUser()->getAttribute('city_id'));
				$crit2=$c->getNewCriterion(BanerPeer::REGION_ID,1);
				$crit1->addOr($crit2);  
        		$c->add($crit1); 
        	}
        }
		
		$c->addAscendingOrderByColumn('RAND()');
		$this->baner = BanerPeer::doSelectOne($c);
		if($this->baner)
		{
		$this->type = $this->Gettype( $this->baner->getFilePath());
		}
		
		//Добавляем показы
		if($this->baner)
		{
		$this->baner->setSee($this->baner->getSee()+1);
		$this->baner->save();

                ////Записываем в лог показы
                $l = new Searchlog();
                $l->setMatId($this->baner->getId());
                $l->setLevel(7);
                $l->save();
                ///

		}
		///
		
	}
	
	public function executeShow_block() {
		
	    $c = new Criteria();
		$c->add(BanerPeer::POSITION_ID,$this->position);
		$c->addAnd(BanerPeer::PUB,true);	
		$this->baners = BanerPeer::doSelect($c);
	}
	
	public function Gettype($name) {
	     $ext = explode('.',$name);
		$ext = $ext[sizeof($ext)-1];
		
			if($ext=='swf')
			{
				return 'flash';
			}
			else 
			{
				return 'image';
			}
	}
	
}
?>